package com.onemile.controls;

import java.util.ArrayList;

import com.onemile.app.R;
import com.onemile.app.UICommodityList;
import com.onemile.app.UILogin;
import com.onemile.app.UIShopList;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.EFavorite;
import com.onemile.entity.model.EShopInfo;
import com.onemile.utils.UtiDialogsFactory;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdpLV_FavoriteList extends BaseAdapter
{
   private ArrayList<EFavorite> mList;
   private LayoutInflater mInflater;
   private Context mContext;
   private AdapterCallBack mCallBack;

   public interface AdapterCallBack
   {
	  public void Do(String UserCollectionId);
   }

   public AdpLV_FavoriteList(Context context, AdapterCallBack callBack)
   {
	  mList = new ArrayList<EFavorite>();
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  mCallBack = callBack;
   }

   public void addItem(ArrayList<EFavorite> _list)
   {
	  if (mList != null)
	  {
		 for (EFavorite eGood : _list)
		 {
			mList.add(eGood);
		 }
	  }
   }

   @Override
   public int getCount()
   {
	  if (mList == null)
	  {
		 return 0;
	  }
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View currentView, ViewGroup parent)
   {
	  try
	  {
		 ViewHolder holder;
		 if (currentView == null)
		 {
			currentView = mInflater.inflate(R.layout.favorite_item, null);
			holder = new ViewHolder();
			holder.ShopName = (TextView) currentView.findViewById(R.id.txtShopName_favorite_item);
			holder.Time = (TextView) currentView.findViewById(R.id.txtTime_favorite_item);
			holder.Recommendation = (TextView) currentView.findViewById(R.id.txtRecommendation_favorite_item);
			holder.ImgGoBuy = (ImageView) currentView.findViewById(R.id.imgGoBuy_favorite_item);
			holder.ImgCancelFav = (ImageView) currentView.findViewById(R.id.imgCancelFav_favorite_item);

			currentView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) currentView.getTag();
		 }

		 holder.ShopName.setText(mList.get(position).getShopName());
		 holder.Time.setText("收藏于" + mList.get(position).getFavoriteTime());
		 holder.Recommendation.setText(mList.get(position).getRecommendation());

		 holder.ImgGoBuy.setTag(position);
		 holder.ImgGoBuy.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{

			   if (Session.getInstance().GetCommodityCount() > 0 || Session.getInstance().getCommodityList().size() > 0)
			   {
				  if (!mList.get((Integer)(v.getTag())).getShopsId().equals(Session.getInstance().getCommodityList().get(0).getShopsId()))
				  {
					 UtiDialogsFactory.getInstance().ShowToastInfoShort(mContext, mContext.getString(R.string.shop_different_commodity));
					 return;
				  }
			   }
			   if (Session.getInstance().GetCommodityCount() == 0 || Session.getInstance().getCommodityList() == null)
			   {
				  EShopInfo shopInfo=new EShopInfo();
				  shopInfo.setClosingTime(mList.get((Integer)(v.getTag())).getClosingTime());
				  shopInfo.setOpeningTime(mList.get((Integer)(v.getTag())).getOpeningTime());
				  shopInfo.setShopsId(mList.get((Integer)(v.getTag())).getShopsId());
				  shopInfo.setShopsName(mList.get((Integer)(v.getTag())).getShopName());
				  shopInfo.setFreight(mList.get((Integer)(v.getTag())).getFreight());
				  shopInfo.setDeliveryDistance(mList.get((Integer)(v.getTag())).getDeliveryDistance());
				  shopInfo.setLowestPrice(mList.get((Integer)(v.getTag())).getLowestPrice());
				  Session.getInstance().setCurrentShop(shopInfo);
			   }

			   Intent intent = new Intent();
			   intent.setClass(mContext, UICommodityList.class);
			   intent.putExtra(PubConst.Intent_Key.SHOP_ID, mList.get((Integer)(v.getTag())).getShopsId());
			   mContext.startActivity(intent);
			}
		 });
		 holder.ImgCancelFav.setTag(position);
		 holder.ImgCancelFav.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{
			   if (Session.getInstance().IsLogin())
			   {
				  mCallBack.Do(mList.get((Integer) v.getTag()).getUserCollectionId());
			   }
			   else
			   {
				  Intent intent = new Intent();
				  intent.setClass(mContext, UILogin.class);
				  intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY, PubConst.ActivityCode.LOGIN);
				  mContext.startActivity(intent);
			   }
			}
		 });
		 return currentView;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "AdpLV_FavoriteList_getView(position, currentView, arg2) ", e);
		 }
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  TextView ShopName;
	  TextView Time;
	  TextView Recommendation;
	  ImageView ImgGoBuy;
	  ImageView ImgCancelFav;
   }

}
